//页面初始加载
efform_onload = function() {
	var fdPlanStatus = decodeURI($("#fdPlanStatus").val());
	//隐藏/显示从表按钮
	if(fdPlanStatus != '草拟')
		hideAndShowSubButtons(false);
	else
		hideAndShowSubButtons(true);
}; 

//从表新增按钮
button_insert_onclick = function (){
	if (isSelected("ef_grid_result") == 0)return; 
	efgrid.submitForm("ef_grid_result", "ZM", "ZMCP11", "insert", true); 
}

//从表修改按钮
button_update_onclick = function (){
	if (isSelected("ef_grid_result") == 0)return; 
	efgrid.submitForm("ef_grid_result", "ZM", "ZMCP11", "update", true);
}

//从表删除按钮
button_delete_onclick = function (){
	var grid = isSelected("ef_grid_result");
	if (grid == 0) return;  
	EFConfirm('你确认要删除吗?', '确认框', function(r) {
		if(r){
			efgrid.submitForm("ef_grid_result", "ZM", "ZMCP11", "delete", true);
		}
	}); 
} 

//从表撤回按钮
button_revert_onclick = function (){
	if (isSelected("ef_grid_result") == 0)return;
	EFConfirm('与所选记录统一批次的数据都会被撤回！', '确认框', function(r) {
		if(r){
			efgrid.submitForm("ef_grid_result", "ZM", "ZMCP11", "revert", true); 
		}
	});
} 

//新增、修改、删除后的执行方法
efgrid_onAjaxSubmitSuccess = function(gridId, service_name, method_name, eiInfo) {
	if (eiInfo.status != -1) {
		var grid = efgrid.getGridObject("ef_grid_result");
		grid.refresh( eiInfo );
		if(service_name=="ZMCP11" && method_name!="query"){
	        parent.refresh_main_grid();			
		}
	}
};

//从表单元格编辑后回调的方法
efgrid_onDataCellSaved = function( grid_id, row_index, col_index, cell_value ) 
{
	var grid = efgrid.getGridObject( grid_id ); 
	if(col_index == 1) 
	{ 
		var info=_getEi();
		var eiBlock=info.getBlock("cmes.cp.workingface");
		for(var i=0;i<eiBlock.getRows().length;i++)
		{
			var _value = eiBlock.getCell(i,"fdKeyId");
			if(_value==cell_value)
			{				  
				var fdRecoveryProcess = eiBlock.getCell(i,"fdRecoveryProcess");	//回采工艺ID <充当落煤方法>
				var fdThickness = eiBlock.getCell(i,"fdThickness");				//平均煤厚
				var fdDensity = eiBlock.getCell(i,"fdDensity");					//容重
				var fdWidth = eiBlock.getCell(i,"fdWidth");						//采长
				var fdHeight = eiBlock.getCell(i,"fdHeight");						//采高
				var fdChance = eiBlock.getCell(i,"fdChance");                     //回采率
				  
				grid.setCellValue(row_index, 3, fdThickness, TYPE_DATA);
				grid.setCellValue(row_index, 4, fdDensity, TYPE_DATA);
				grid.setCellValue(row_index, 5, fdWidth, TYPE_DATA);
				grid.setCellValue(row_index, 6, fdHeight, TYPE_DATA);
				grid.setCellValue(row_index, 8, fdChance, TYPE_DATA);
				grid.setCellValue(row_index, 10, fdRecoveryProcess, TYPE_DATA);

				grid.refreshCell( row_index, 3 , TYPE_DATA );
				grid.refreshCell( row_index, 4 , TYPE_DATA );
				grid.refreshCell( row_index, 5 , TYPE_DATA );
				grid.refreshCell( row_index, 6 , TYPE_DATA );
				grid.refreshCell( row_index, 8 , TYPE_DATA );
				grid.refreshCell( row_index, 10 , TYPE_DATA );
			}  
		}
	}
	  
	//设置计划产量总值
	var fdHeigh = grid.getCellValue(row_index, 6,"fdHeight");	//采高
	var fdWidth = grid.getCellValue(row_index,5, "fdWidth");	//采长
	var fdDensit = grid.getCellValue(row_index,4, "fdDensity");//容重
	var fdBasedIn = grid.getCellValue(row_index, 7,"fdBasedIn");//推进
	var fdChance = grid.getCellValue(row_index, 8,"fdChance")/100;//回采率
	var fdPlanSum = (Number(fdBasedIn)*Number(fdHeigh)*Number(fdWidth)*Number(fdDensit)*Number(fdChance));
	grid.getBlockData().setCell(row_index, "fdPlanSum", parseInt(fdPlanSum));
	grid.refreshCell( row_index, 9 , TYPE_DATA );
	//煤层面积计算
	var fdThickness = grid.getCellValue(row_index,3,"fdThickness");//煤厚
	if(Number(fdThickness)<1.5){
		//薄煤层面积
		var fdBhcmj = (Number(fdWidth) * Number(fdBasedIn));
		grid.getBlockData().setCell(row_index, "fdBhcmj", parseInt(fdBhcmj));
		grid.refreshCell( row_index, 12 , TYPE_DATA );
		grid.getBlockData().setCell(row_index, "fdZhcmj", 0);
		grid.refreshCell( row_index, 13 , TYPE_DATA );
		//薄煤层产量
		grid.getBlockData().setCell(row_index, "fdBhcsl",parseInt(fdPlanSum));
		grid.refreshCell( row_index, 14 , TYPE_DATA );
		grid.getBlockData().setCell(row_index, "fdZhcsl",0);
		grid.refreshCell( row_index, 15 , TYPE_DATA );
	}
	else{
		//中厚煤层
		var fdZhcmj = (Number(fdWidth) * Number(fdBasedIn));
		grid.getBlockData().setCell(row_index, "fdZhcmj", parseInt(fdZhcmj));
		grid.refreshCell( row_index, 13 , TYPE_DATA );
		grid.getBlockData().setCell(row_index, "fdBhcmj", 0);
		grid.refreshCell( row_index, 12 , TYPE_DATA );
		//中厚煤层产量
		grid.getBlockData().setCell(row_index, "fdZhcsl",parseInt(fdPlanSum));
		grid.refreshCell( row_index, 15 , TYPE_DATA );
		grid.getBlockData().setCell(row_index, "fdBhcsl",0);
		grid.refreshCell( row_index, 14 , TYPE_DATA );
	}
}

/*****************公用方法区******************/
//根据主表选中记录的状态，确定从表的按钮是否显示
var hideAndShowSubButtons=function(status)
{
	if(!status)
	{
		efbutton.setButtonStatus("insert", false);
		efbutton.setButtonStatus("update", false);
		efbutton.setButtonStatus("delete", false);
		efbutton.setButtonStatus("revert", false);
		efbutton.setButtonStatus("upload",false);
	}else
	{
		efbutton.setButtonStatus("insert", true);
		efbutton.setButtonStatus("update", true);
		efbutton.setButtonStatus("delete", true);
		efbutton.setButtonStatus("revert", true);
		efbutton.setButtonStatus("upload", true);
	}
}

efgrid_afterAddNewRow = function(grid_id) {
	var grid = efgrid.getGridObject(grid_id);
	var rows = grid.getCheckedRows();
	for (var i = 0; i < rows.length; i++) {
		grid.getBlockData().setCell(rows[i], "fdKeyId", "");
	}
};

